<template>
    
    <!-- 组件的模板 -->
    <!-- <div class="weui-cells__title">选择时间段</div> -->
     <div class="weui-cells">
        <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectStartTime()">
            <div class="weui-cell__bd">
                <p>开始时间</p>
            </div>
            <div class="weui-cell__ft">
                <span class="placeholder" v-if="start_date == ''">请选择</span>{{start_date}}
            </div>
        </a>
        <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectEndTime()">
            <div class="weui-cell__bd">
                <p>结束时间</p>
            </div>
            <div class="weui-cell__ft">
                <span class="placeholder" v-if="end_date == ''">请选择</span>{{end_date}}
            </div>
        </a>
    </div>
    <div class="mod-btn-box">
        <a href="javascript:;" 
            class="weui-btn weui-btn_primary"
            :class="{'weui-btn_disabled': !isValidation}"
            @click="exportSign"
            >确认导出</a>
    </div>
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import clientUi from 'gapi/clientUi.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    import socialApi from 'clientApi/socialApi.js'
    import widget from 'gapi/widget.js'
    import commonPage from 'clientApi/commonPage.js'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                start_date: '',
                end_date: '',
                timestr: '',
                today: '',
                export_sign_info: {}
            }
        },
        computed:{
            minDate: function(){
                return util.getLocalTime(this.timestr - 366 * 24 * 60 * 60 * 1000, 'yyyy-MM-dd')
            },
            isValidation () {
                return !!this.start_date && !!this.end_date
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                //今天日期
                let date = new Date()
                this.timestr = date.getTime()
                this.today = util.getLocalTime(this.timestr, 'yyyy-MM-dd')
                // alert(this.minDate)
                //获取上页数据
                // alert(location.href) 
                //数据来源  sign / form
                this.origin_page = util.getUrddataString('origin_page')
                if(this.origin_page === "sign"){
                    document.title = '位置数据导出'
                    cache.getCache('export_sign_info_cache', (obj) => {
                        // alert(JSON.stringify(obj))
                        this.export_sign_info = obj
                    })
                }else if(this.origin_page === "form"){
                    document.title = '数据导出'
                    this.export_sign_info = JSON.parse(util.getUrddataString('export_sign_info'))
                }
                
                
                // alert(JSON.stringify(this.export_sign_info))
            },
            selectStartTime(){
                //以前版本导出规则（勿删）
                // KCApiApp.showDatePicker(start, this.end_date || this.today, this.start_date || this.today, (data) => {
                //     this.start_date = data.SelectedDate
                // }, '导出时间范围不能超过一个月')

                let start = ''
                if(this.end_date){
                    start = util.getLocalTime(util.getStampFromDate(this.end_date) - 31 * 24 * 60 * 60 * 1000, 'yyyy-MM-dd')
                    if(start <= this.minDate){
                        start = this.minDate
                    }
                }else{
                    start = this.minDate
                }
                console.log("start:"+start)
                KCApiApp.showDatePicker("1990-01-01",  this.today, this.start_date || this.today, (data) => {
                    this.start_date = data.SelectedDate
                }, '导出时间范围不能超过一个月')



            },
            selectEndTime(){
                //以前版本导出规则（勿删）
                // KCApiApp.showDatePicker(this.start_date || this.minDate, end, this.end_date || this.today, (data) => {
                //     this.end_date = data.SelectedDate
                // }, '导出时间范围不能超过一个月')

                let end = ''
                if(this.start_date){
                    end = util.getLocalTime(util.getStampFromDate(this.start_date) + 31 * 24 * 60 * 60 * 1000, 'yyyy-MM-dd')
                    if(end >= this.today){
                        end = this.today
                    }
                }else{
                    end = this.today
                }
                console.log('end:'+end)
                KCApiApp.showDatePicker("1990-01-01", this.today, this.end_date || this.today, (data) => {

                    this.end_date = data.SelectedDate
                }, '导出时间范围不能超过一个月')
                    // widget.toast('请先选择起始日期')

            },
            exportSign(){
                if(!this.start_date || !this.end_date){
                    widget.toast('请选择导出区间')
                    return
                }
                if(util.getStampFromDate(this.end_date) < util.getStampFromDate(this.start_date)){
                    widget.toast('结束时间不得小于开始时间')
                    return
                }
                if((util.getStampFromDate(this.end_date) - util.getStampFromDate(this.start_date)) > (31 * 24 * 60 * 60 * 1000)){
                     widget.toast('导出时间范围不能超过一个月')
                     return
                }
                let url = ''
                let data = {}
                if(this.origin_page === "sign"){
                    url = '/sign/project/exportsign'
                    data = {
                        team_id: this.export_sign_info.team_id,
                        project_id: this.export_sign_info.project_id,
                        start_date: this.start_date,
                        end_date: this.end_date,
                        userid_list: JSON.stringify(this.export_sign_info.userid_list)
                    }
                    // console.log('sign')
                    socialApi.exportDataFileAndShare(2, data)
                }else if(this.origin_page === "form"){
                    //埋点
                    let statisticsLog_data = {
                        event_id: '数据导出-确认导出',
                        // event_label: data.event_label,(预留，现在不用传此参数)
                        event_param: {
                            ca_from: 'sure',
                            project_id: this.export_sign_info.project_id,
                        }
                    }
                    // alert(JSON.stringify(statisticsLog_data))
                    KCApiApp.statisticsLog(statisticsLog_data)
                    
                    url = '/form/report/list/export2'
                    data = {
                        team_id: this.export_sign_info.team_id,
                        project_id: this.export_sign_info.project_id,
                        start_date: this.start_date,
                        end_date: this.end_date,
                        form_id: this.export_sign_info.form_id
                    }
                    // console.log("form")
                    socialApi.exportDataFileAndShare(3, data)
                }
                console.log(JSON.stringify(data))

                // return
                // commonPage.loadPageStatus('loading')
                // util.ajax({
                //     url: url,
                //     type: 'GET',
                //     data: data,
                //     timeout: 10000,
                //     success: (data) => {
                //         commonPage.loadPageStatus('loadSuccess')
                //         // alert(JSON.stringify(data))
                //         console.log(data)
                //         // success
                //         if(data.errno == 0){
                //             if(data.data.xls_url){
                //                 clientUi.alertDialog('导出签到', '正在生成报表，稍后将在项目消息中发送下载链接，请注意查收', () => {
                //                     // alert(1)
                //                     commonPage.goBack()
                //                 })
                //             }else{
                //                 widget.toast('所选时间段内无签到记录,请重新选择')
                //             }
                            
                //         }else if(data.errno == 24001){
                //             widget.toast('一次最多导出一个月的签到记录')
                //         }else if(data.errno == 24002){
                //             widget.toast('只能导出一年内的签到记录')
                //         }else{
                //             // widget.toast('服务器异常')
                //             widget.toast(data.errmsg)
                //             // alert(JSON.stringify(data))
                //         }
                //         // alert(JSON.stringify(data))
                //     },
                //     error: (xhr, status) => {
                //         // error
                //         console.log(xhr)
                //         widget.toast('网络连接失败，请检查网络')
                //         commonPage.loadPageStatus('loadSuccess')
                //     },
                //     noNetwork: () => {
                //         // 无网络的回调
                //         widget.toast('网络连接失败，请检查网络')
                //         commonPage.loadPageStatus('loadSuccess')
                //     }
                // })
            }
        },
        created(){

            this.init();
            // commonPage.updateTitleBar({
            //     //backAction: 'checkChange',    // 点击返回按钮的动作（即回调函数名称）
            //     // leftDisplay: false,            // 是否显示左按钮
            //     // leftText: '',                // 左按钮文案
            //     //leftAction: 'checkChange',    // 点击左按钮的动作（即回调函数名称）
            //     rightDisplay: true,            // 是否显示右按钮
            //     rightText: '确认导出',            // 右按钮文案
            //     rightAction: 'exportSign'    // 点击右按钮的文案（即回调函数名称）
            // })
            // window.exportSign = this.exportSign
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/attendance.css"></style>



